package com.bootdo.system.dao;

import com.bootdo.system.domain.Parents;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ParentsMapper {

	@Select("select * from tb_parents where id = #{id}")
	Parents get(Long id);
	
	@Select("select * from tb_parents where openId = #{openId}")
	Parents getByOpenId(String openId);
	
	@Select("select sum(coursePrice) from tb_order where parentsId = #{id} and orderStatus=2 ")
	Double getConsumerTotal(Long id);
	
	
	@Select("select count(id) from tb_order where parentsId = #{id} and orderStatus=2")
	Integer getOrderNums(Long id);
	
	@Select("<script>" +
	"select * from tb_parents " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"name != null and name != ''\">"+ "and name = #{name} " + "</if>" + 
	  		  "<if test=\"nickName != null and nickName != ''\">"+ "and nickName = #{nickName} " + "</if>" + 
	  		  "<if test=\"country != null and country != ''\">"+ "and country = #{country} " + "</if>" + 
	  		  "<if test=\"province != null and province != ''\">"+ "and province = #{province} " + "</if>" + 
	  		  "<if test=\"city != null and city != ''\">"+ "and city = #{city} " + "</if>" + 
	  		 "<if test=\"openId != null and openId != ''\">"+ "and openId = #{openId} " + "</if>" + 
		  			"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<Parents> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select ifnull(count(*),0) from tb_parents " + 
				"<where>" + 
			  		  "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
			  		  "<if test=\"name != null and name != ''\">"+ "and name = #{name} " + "</if>" + 
			  		  "<if test=\"nickName != null and nickName != ''\">"+ "and nickName = #{nickName} " + "</if>" + 
			  		  "<if test=\"country != null and country != ''\">"+ "and country = #{country} " + "</if>" + 
			  		  "<if test=\"province != null and province != ''\">"+ "and province = #{province} " + "</if>" + 
			  		  "<if test=\"city != null and city != ''\">"+ "and city = #{city} " + "</if>" + 
			  		 "<if test=\"openId != null and openId != ''\">"+ "and openId = #{openId} " + "</if>" + 
			  	"</where>"+ 
			"</script>")
	int getTotal(Map<String,Object> map);
	
	@Insert("insert into tb_parents (`name`, `nickName`, `country`, `province`, `city`, `openId`, `createUserId`, `createTime`,`gender`,`phone`,`unionId`)"
	+ "values (#{name}, #{nickName}, #{country}, #{province}, #{city}, #{openId},#{createUserId}, #{createTime}, #{gender}, #{phone}, #{unionId})")
	@Options(useGeneratedKeys=true)
	int save(Parents bComments);
	
	@Update("<script>"+ 
			"update tb_parents " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"name != null\">`name` = #{name}, </if>" + 
                    "<if test=\"nickName != null\">`nickName` = #{nickName}, </if>" + 
                    "<if test=\"country != null\">`country` = #{country}, </if>" + 
                    "<if test=\"province != null\">`province` = #{province}, </if>" + 
                    "<if test=\"city != null\">`city` = #{city}, </if>" +    
                    "<if test=\"openId != null\">`openId` = #{openId}, </if>" +    
                    "<if test=\"phone != null\">`phone` = #{phone}, </if>" +
                    "<if test=\"unionId != null\">`unionId` = #{unionId}, </if>" +
          					"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(Parents bComments);
	
	@Delete("delete from tb_parents where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_parents where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
}
